package com.example.triprec.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.triprec.R;
import com.example.triprec.entity.Scenic;
import com.example.triprec.entity.Trip;
import com.gzone.university.adapter.ItemViewHolder;
import com.gzone.university.adapter.ViewAdapter;
import com.gzone.university.utils.DateUtils;
import com.gzone.university.utils.GlideUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class TripAdapter extends ViewAdapter<TripAdapter.TripViewHolder> {

    private List<Trip> mTripList = new ArrayList<>();
    private OnTripClickListener mListener;

    public TripAdapter(View view) {
        super(view);
    }

    @Override
    public TripViewHolder onCreateViewHolder(@NonNull ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_trip, parent, false);
        return new TripViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TripViewHolder holder) {
        Trip trip = mTripList.get(holder.getAdapterPosition());
        Scenic scenic = trip.getScenic();

        // 设置行程标题
        holder.tvTripName.setText(trip.getTripName());

        // 设置行程日期
        String dateRangeText = trip.getStartDate();
        if (trip.getEndDate() != null && !trip.getEndDate().isEmpty()) {
            dateRangeText += " - " + trip.getEndDate();
        }
        holder.tvTripDate.setText(dateRangeText);

        // 设置地点
        if (scenic != null) {
            holder.tvTripLocation.setText(scenic.getLocation());
            // 设置景点图片
            GlideUtils.load(holder.ivTripImage, scenic.getImageResources()[0]);
        }

        // 计算天数
        String daysText = calculateDaysText(trip.getStartDate(), trip.getEndDate());
        holder.tvTripDays.setText(daysText);

        if (trip.getStatus() == 1) {
            if (trip.getComment() == null) {
                holder.btnFinish.setText("添加评论");
                holder.btnFinish.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null) {
                            mListener.onTripCommentClick(trip, holder.getAdapterPosition());
                        }
                    }
                });
            } else {
                holder.btnFinish.setText("查看评论");
                holder.btnFinish.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null) {
                            mListener.onTripShowCommentClick(trip, holder.getAdapterPosition());
                        }
                    }
                });
            }
            holder.btnEdit.setVisibility(View.GONE);
        } else {
            holder.btnFinish.setText("结束行程");
            holder.btnEdit.setVisibility(View.VISIBLE);
            holder.btnEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onTripEditClick(trip, holder.getAdapterPosition());
                    }
                }
            });
            // 设置点击事件
            holder.btnFinish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onTripFinishClick(trip, holder.getAdapterPosition());
                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mTripList.size();
    }

    public void setData(List<Trip> tripList) {
        this.mTripList = tripList;
        notifyDataSetChanged();
    }

    public void setOnTripClickListener(OnTripClickListener listener) {
        this.mListener = listener;
    }

    /**
     * 计算旅行天数文本
     */
    private String calculateDaysText(String startDate, String endDate) {
        if (startDate == null || startDate.isEmpty()) {
            return "未设置天数";
        }

        if (endDate == null || endDate.isEmpty()) {
            return "1天";
        }

        Date start = DateUtils.parse(startDate, "yyyy-MM-dd");
        Date end = DateUtils.parse(endDate, "yyyy-MM-dd");

        if (start != null && end != null) {
            long diffInMillies = Math.abs(end.getTime() - start.getTime());
            long daysDiff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS) + 1;

            if (daysDiff <= 1) {
                return "1天";
            } else {
                return daysDiff + "天" + (daysDiff - 1) + "晚";
            }
        }

        return "未设置天数";
    }

    public interface OnTripClickListener {
        void onTripFinishClick(Trip trip, int position);

        void onTripEditClick(Trip trip, int position);

        void onTripCommentClick(Trip trip, int position);

        void onTripShowCommentClick(Trip trip, int position);
    }

    public static class TripViewHolder extends ItemViewHolder {
        ImageView ivTripImage;
        TextView tvTripName;
        TextView tvTripDate;
        TextView tvTripLocation;
        TextView tvTripDays;
        View btnEdit;
        Button btnFinish;

        public TripViewHolder(@NonNull View itemView) {
            super(itemView);
            ivTripImage = itemView.findViewById(R.id.iv_trip_image);
            tvTripName = itemView.findViewById(R.id.tv_trip_name);
            tvTripDate = itemView.findViewById(R.id.tv_trip_date);
            tvTripLocation = itemView.findViewById(R.id.tv_trip_location);
            tvTripDays = itemView.findViewById(R.id.tv_trip_days);
            btnEdit = itemView.findViewById(R.id.btn_edit);
            btnFinish = itemView.findViewById(R.id.btn_finish);
        }
    }
} 